#include "weatherinfo.h"


WeatherInfo::WeatherInfo(QObject *parent) : QObject(parent),
    m_pm25(0), m_pm10(0)
{
    
}

// 初始化天气类型与图标路径的映射
const QMap<QString, QString> WeatherInfo::weatherIconMap = {
    {"晴", ":/img/weather/sunny.png"},
    {"多云", ":/img/weather/cloudy.png"},
    {"阴", ":/img/weather/overcast.png"},
    {"小雨", ":/img/weather/small_rain.png"},
    {"中雨", ":/img/weather/moderate_rain.png"},
    {"大雨", ":/img/weather/heavy_rain.png"},
    {"雷阵雨", ":/img/weather/thundershower.png"},
    {"默认", ":/img/weather/air_quality.png"} // 未匹配时使用
};

// 加载 JSON 字符串并解析
bool WeatherInfo::loadFromJson(const QString &json)
{
    QJsonParseError error;
    QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8(), &error);

    if (error.error != QJsonParseError::NoError || !doc.isObject())
        return false;

    QJsonObject root = doc.object();

    // 获取城市信息
    QJsonObject cityInfo = root["cityInfo"].toObject();
    m_city = cityInfo["city"].toString();
    m_updateTime = cityInfo["updateTime"].toString();

    // 获取日期
    m_date = root["date"].toString();

    // 获取今日数据
    QJsonObject data = root["data"].toObject();
    m_shidu = data["shidu"].toString();
    m_wendu = data["wendu"].toString();
    m_pm25 = data["pm25"].toDouble();
    m_pm10 = data["pm10"].toDouble();
    m_quality = data["quality"].toString();
    m_ganmao = data["ganmao"].toString();
    
    

    // 获取未来7天天气
    m_forecast.clear();
    QJsonArray forecastArray = data["forecast"].toArray();
    for (int i = 0; i < 7 && i < forecastArray.size(); ++i)
    {
        QJsonObject obj = forecastArray[i].toObject();
        Forecast f;
        f.high = obj["high"].toString();
        f.low = obj["low"].toString();   
        f.ymd = obj["ymd"].toString();
        f.week = obj["week"].toString();
        f.sunrise = obj["sunrise"].toString();
        f.sunset = obj["sunset"].toString();
        f.aqi = obj["aqi"].toInt();
        f.fx = obj["fx"].toString();
        f.fl = obj["fl"].toString();
        f.type = obj["type"].toString();
        f.notice = obj["notice"].toString();
        
        m_forecast.append(f);
    }


    
    // 打印信息
       qDebug() << "天气类型:" << m_type;
       qDebug() << "城市:" << m_city;
       qDebug() << "日期:" << m_date;
       qDebug() << "更新时间:" << m_updateTime;
       qDebug() << "温度:" << m_wendu << "湿度:" << m_shidu;
       qDebug() << "空气质量:" << m_quality << "PM2.5:" << m_pm25 << "PM10:" << m_pm10;
       qDebug() << "适宜人群:" << m_ganmao;

       for (int i = 0; i < 7 && i < m_forecast.size(); ++i) 
       {
           const Forecast &f = m_forecast[i];  // 使用已初始化的对象
           qDebug() << "===";
           qDebug() << f.ymd << f.week << f.type;
           qDebug() << "温度:" << f.low << "~" << f.high;
           qDebug() << "日出:" << f.sunrise << "日落:" << f.sunset;
           qDebug() << "风:" << f.fx << f.fl;
           qDebug() << "AQI:" << f.aqi;
           qDebug() << "提示:" << f.notice;
           qDebug() << f.high << f.low;
       }
    
    
    return true;
}

// 获取天气图标
QPixmap WeatherInfo::getWeatherIcon(const QString &weatherType, int size) const {
    //若找到匹配的 weatherType，返回对应图标路径；否则返回默认图标路径
    qDebug() << "Getting weather icon for:" << weatherType; // 调试输出
    QString iconPath = weatherIconMap.value(weatherType, ":/img/weather/sunny.png");
    QPixmap icon(iconPath);
    
    if (icon.isNull()) {
        qWarning() << "Failed to load weather icon:" << iconPath;
        icon = QPixmap(":/img/weather/air_quality.png"); // 确保返回默认图标
    }
    
    return icon.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

// 获取当前天气图标
QPixmap WeatherInfo::getCurrentWeatherIcon(int size) const {
     const Forecast today = m_forecast.first();
    return getWeatherIcon(today.type, size);
}

// 获取未来天气预报图标
QPixmap WeatherInfo::getForecastWeatherIcon(int index, int size) const {
    if (index >= 0 && index < m_forecast.size()) {
        return getWeatherIcon(m_forecast[index].type, size);
    }
    return getWeatherIcon("默认", size);
}

// Getter 接口实现

QString WeatherInfo::city() const
{
    return m_city;
}

QString WeatherInfo::date() const
{
    return m_date;
}

QString WeatherInfo::updateTime() const
{
    return m_updateTime;
}

QString WeatherInfo::shidu() const
{
    return m_shidu;
}

QString WeatherInfo::wendu() const
{
    return m_wendu;
}

double WeatherInfo::pm25() const
{
    return m_pm25;
}

double WeatherInfo::pm10() const
{
    return m_pm10;
}

QString WeatherInfo::quality() const
{
    return m_quality;
}

QString WeatherInfo::ganmao() const
{
    return m_ganmao;
}

QList<Forecast> WeatherInfo::forecastList() const
{
    return m_forecast;
}
